package yxy.game.pm2.active.command.celebration;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.acttreasuredata;
import yxy.cherry.data.bean.acttreasurefloprandom;
import yxy.cherry.data.bean.acttreasurestepreward;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_国庆寻宝;
import yxy.game.pm2.active.module.subordinateclass.flop.FlopCard;
import yxy.game.pm2.bean.Constant;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.Item;
import yxy.game.pm2.bean.currency.CurrencyType;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.NationalDayMessage;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 国庆寻宝信息ui
 *
 * @ClassName CMD_Active_国庆寻宝_NationalDayLotteryInfo
 * @Author jingtao-zheng
 * @Date 2022/9/21 16:14
 * @Version 1.0
 **/
@MessageType(proto = Command.NationalDayLotteryInfo, classtype = NationalDayMessage.NationalDayLotteryInfoRequest.class)
public class CMD_Active_国庆寻宝_NationalDayLotteryInfo extends Command<NationalDayMessage.NationalDayLotteryInfoRequest> {

    @Override
    public void execute(MessageSource messageSource, NationalDayMessage.NationalDayLotteryInfoRequest request) throws Exception {
        String playercode = messageSource.getTag();
        NationalDayMessage.NationalDayLotteryInfoResponse.Builder response = NationalDayMessage.NationalDayLotteryInfoResponse.newBuilder();
        try {
            ActiveModule_国庆寻宝 am = ActiveModule_国庆寻宝.get(playercode, false);
            if (am == null) {
                send(messageSource, response, Result.ActNoOpen);
                return;
            }

            if (!am.running()) {
                send(messageSource, response, Result.ActNoOpen);
                return;
            }

            //当前层数
            response.setFloor(am.getFloor());
            //全部层奖励
            Map<String, acttreasurestepreward> allData = DataCenter.getAllData(acttreasurestepreward.class);
            for (acttreasurestepreward value : allData.values()) {
                NationalDayMessage.NationalDayLotteryReward.Builder rwds = NationalDayMessage.NationalDayLotteryReward.newBuilder();
                rwds.setId(value.getId());//表id
                Collection<Currency> currencies = Currency.create(value.getRewardtype(), value.getRewardid(), value.getRewardnum());
                Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(currencies);
                rwds.addAllRewards(items);//奖品
                rwds.setFloor(value.getStep());//对应层数
                ArrayList<Integer> floorAward = am.getFloorAward();
                boolean contains = floorAward.contains(value.getId());
                rwds.setIsGot(contains);//是否已领取
                rwds.setType(value.getRewardlv());
                response.addRwds(rwds);
            }
            //当前格子信息-只发打开的格子信息
            Map<String, acttreasurefloprandom> allDataRandom = DataCenter.getAllData(acttreasurefloprandom.class);
            List<FlopCard> flopCardList = am.getFcmap().values().stream().filter(_flopCard -> _flopCard.isFlop()).collect(Collectors.toList());
            for (FlopCard flopCard : flopCardList) {
                NationalDayMessage.NationalDayLotteryCell.Builder nodeInfo = NationalDayMessage.NationalDayLotteryCell.newBuilder();
                nodeInfo.setPos(flopCard.getPosition());
                acttreasurefloprandom acttreasurefloprandom = allDataRandom.get(String.valueOf(flopCard.getId()));
                Currency currency = Currency.create(acttreasurefloprandom.getRewardtype(), acttreasurefloprandom.getRewardid(), acttreasurefloprandom.getRewardnum());
                if(currency!=null){
                    PackageMessage.Item item = new CurrencyHandle().packItem(currency);
                    nodeInfo.setReward(item);
                }
                nodeInfo.setNext(flopCard.isGrand());
                response.addCells(nodeInfo);
            }
            //寻宝道具
            acttreasuredata data = DataCenter.getData(1, acttreasuredata.class);
            Currency treasureitem = Item.get(playercode, CurrencyType.CT_物品, data.costid);    //寻宝道具
            PackageMessage.Item item = new CurrencyHandle().packItem(treasureitem);
            response.setCostItem(item);
            //结束时间戳
            long millis = am.getTimeline().getEndTime().getMillis();
            response.setEndTime(millis);
            //格子奖池
            List<acttreasurefloprandom> oneTypeRandom = allDataRandom.values().stream().filter(_random -> _random.getType() == Constant.ONE).collect(Collectors.toList());
            for (yxy.cherry.data.bean.acttreasurefloprandom acttreasurefloprandom : oneTypeRandom) {
                Currency currency = Currency.create(acttreasurefloprandom.getRewardtype(), acttreasurefloprandom.getRewardid(), acttreasurefloprandom.getRewardnum());
                PackageMessage.Item itemPool = new CurrencyHandle().packItem(currency);
                response.addPool(itemPool);
            }
            send(messageSource, response, Result.Success);
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        }
    }

    private void send(MessageSource messageSource, NationalDayMessage.NationalDayLotteryInfoResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}
